package com.alcatel_lucent.osp.action;

import com.alcatel_lucent.osp.model.Comment;
import com.alcatel_lucent.osp.model.Request;
import com.alcatel_lucent.osp.service.RequestService;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * User: guoshunw
 * Date: 13-8-27
 * Time: 下午3:24
 */

@ParentPackage("osp-json")
@Result(type = "json", params = {
        "noCache", "true",
        "contentType", "json",
        "enableGZIP ", "true",
        "ignoreHierarchy", "false",
        "root", "request",
        "excludeNullProperties", "true",

        "excludeProperties", "^.*attachments\\[\\d+\\]\\.content$, " +
        "(?:type|lastUpdateBy|reportBy|(?:comments\\[\\d+\\].creator)|copyList\\[\\d+\\]).categories.*, " +
        "category.(?:owner|type)s.*, "
})
public class GetRequestAction extends BaseAction {
    private Long id;
    private Request request;

    public Request getRequest() {
        return request;
    }

    public void setRequest(Request request) {
        this.request = request;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Autowired
    private RequestService requestService;

    @Override
    public String execute() throws JSONException {
        request = requestService.findRequestById(id);

        Collections.sort((List<Comment>) request.getComments(), new Comparator<Comment>() {
            @Override
            public int compare(Comment c1, Comment c2) {
                return (int) (c2.getCreateTime().getTime() - c1.getCreateTime().getTime());
            }
        });

        log.info("request ={}", request);
        return SUCCESS;
    }
}
